<template>
  <div>
    <!-- 91-1-1 订单列表：通过路由渲染订单列表页面--- 面包屑布局头部导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- //91-1-1 创建Order.vue文件，并渲染文件内容----卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 93-1-1订单列表-渲染订单table表格 -->
      <!-- 订单列表数据 -->
      <!-- border显示边框 stripe隔行变色 -->
      <el-table :data="orderlist" border stripe>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款" prop="pay_status">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row == '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template slot-scope="scope">
            <!-- | dateFormat 调用一个全局的过滤器 -->
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="showBox"
          ></el-button>
          <el-button
            size="mini"
            type="success"
            icon="el-icon-location"
            @click="showProgressBox"
          ></el-button>
        </el-table-column>
      </el-table>
      <!-- 94-1-1:订单列表-实现分页功能 -->
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 95-1-1-2订单列表-实现省市区县数据联动效果--使用对话框组件，
      表单校验组件，级联选择器基础组件进行搭建 -->
      <!-- 修改地址对话框 -->
      <el-dialog
        title="修改地址"
        :visible.sync="addressVisible"
        width="50%"
        @close="addressDialogClosed"
      >
        <el-form
          :model="addressForm"
          :rules="addressFormRules"
          ref="addressFormRef"
          label-width="100px"
        >
          <!-- 级联选择器数据关联的数据:options="cityData" -->
          <!-- addressForm.address2 表单关联的数据 -->
          <el-form-item label="省/区县" prop="address1">
            <el-cascader
              v-model="addressForm.address1"
              :options="cityData"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="addressVisible = false">取 消</el-button>
          <el-button type="primary" @click="addressVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 96-1-2:订单列表-展示物流进度对话框并获取物流信息--使用时间组件 -->
      <!-- 展示物流进度的对话框 -->
      <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
        <!-- 时间线 -->
        <el-timeline>
          <!-- activity：获得数据中的每一个数据   index：索引   progressInfo:数据-->
          <!-- time 表示数据中每一个对象中的time -->
          <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            :timestamp="activity.time"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
//为什么要在这里导入js
import cityData from './citydata.js'
export default {
  data() {
    return {
      //92-1-1订单列表-根据获取订单数据列表
      // 查询对象
      queryInfo: {
        // 查询条件，查询参数
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      orderlist: [],
      addressVisible: false,
      //这个数据干嘛用的
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {},
      //引用js的文件
      cityData: cityData,
      //控制物流对话框显示
      progressVisible: false,
      //物流信息数据
      progressInfo: []
    }
  },
  created() {
    this.getOrderList()
  },
  //   methods节点
  methods: {
    //92-1-2订单列表-根据获取订单数据列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      console.log('res====')
      console.log(res)
      console.log('res====')
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败！')
      }
      console.log(res)
      this.total = res.data.total
      this.orderlist = res.data.goods
    },
    // 94-1-2:订单列表-实现分页功能
    // 只要pagesize值发生变化，系统会自动调用这个方法
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 94-1-3:订单列表-实现分页功能
    //只要pagenum值发生变化就会调用这个方法
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 95-1-1-1订单列表-实现省市区县数据联动效果--添加编辑事件showBox
    showBox() {
      this.addressVisible = true
    },
    //95-1-1-3订单列表-实现省市区县数据联动效果-- 取消或者关闭清空表单内容
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    // 96-1-1:订单列表-展示物流进度对话框并获取物流信息
    async showProgressBox() {
      const { data: res } = await this.$http.get('/kuaidi/777040080610631')

      if (res.meta.status !== 200) {
        return this.$message.error('获取物流进度失败！')
      }

      this.progressInfo = res.data

      this.progressVisible = true
      console.log(this.progressInfo)
    }
  }
}
</script>
<style lang="less" scoped>
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';
.el-cascader {
  width: 100%;
}
</style>
